using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL
{
    public class SystemControl
    {
        RapidHardware RapidHardware;

        public bool IsClockRunning()
        {
            if (RapidHardware.Settings.RunFromXilinx)
                 return RapidHardware.FPGAInterface.IsClockRunning();
            return false;
        }

        public SystemControl(RapidHardware poRapidHardware)
        {
            RapidHardware = poRapidHardware;
        }

        public string Start()
        {
            if (RapidHardware.Settings.RunFromXilinx)
                return RapidHardware.FPGAInterface.Start();
            return "Starting Simulation";
        }

        public bool Reset()
        {
            if (!RapidHardware.Settings.UseReset)
                return false;

            if (RapidHardware.Settings.RunFromXilinx)
                return RapidHardware.FPGAInterface.Reset();

            RapidHardware.SystemInterface.SetSignal("MAIN_RESET", 1);
            Go(4);
            RapidHardware.SystemInterface.SetSignal("MAIN_RESET", 0);
            return true;
        }

        public bool Go(int piClockCycles)
        {
            if (!RapidHardware.Settings.UseClock)
                return false;

            if (RapidHardware.Settings.RunFromXilinx)
                return RapidHardware.FPGAInterface.Go(piClockCycles);

            RapidHardware.Simulation.Clock.DoClock(piClockCycles);
            return true;
        }

        public bool StartClock()
        {
            if (!RapidHardware.Settings.UseClock)
                return false;

            if (RapidHardware.Settings.RunFromXilinx)
            {
                return RapidHardware.FPGAInterface.StartClock();
            }
            else
                return false;  // not supported
        }

        public bool StopClock()
        {
            if (!RapidHardware.Settings.UseClock)
                return false;

            if (RapidHardware.Settings.RunFromXilinx)
                return RapidHardware.FPGAInterface.StopClock();
            else
                return false;  // not supported
        }


        public bool Stop()
        {
            if (RapidHardware.Settings.RunFromXilinx)
                return RapidHardware.FPGAInterface.Stop();

            return true;
        }

        public long ClockCycles()
        {
            if (RapidHardware.Settings.RunFromXilinx)
                return RapidHardware.FPGAInterface.ClockCycles;

            return (int)(RapidHardware.Simulation.Clock.TimeNow * RapidHardware.Simulation.Clock.Frequency);

        }

    }
}
